package com.qyer.search.searchplan;

import com.google.common.collect.Lists;
import com.qyer.search.Constants;
import com.qyer.search.cache.mutable.MutableCaches;
import com.qyer.search.exception.SearchException;
import com.qyer.search.model.ExtendVType;
import com.qyer.search.model.SearchParameter;
import com.qyer.search.model.SearchParameterEntity;
import com.qyer.search.model.SearchParameterOrigin;
import com.qyer.search.model.middle.ExtendEntity;
import com.qyer.words.model.Impression;
import com.qyer.words.model.Word;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * User: Z J Wu Date: 2016/01/27 Time: 00:39 Package: com.qyer.search.searchplan
 */
public class SQPlanner {

  public static List<SQPlan> makePlan(SearchParameterOrigin sp, Set<List<Word>> words) throws
    Exception {
    List<SearchParameter> searchParameters = sp.splitByType();
    int size = searchParameters.size();
    List<SQPlan> plans = new ArrayList<>();
    if (size == 0) {
      return plans;
    } else {
      if (CollectionUtils.isEmpty(words)) {
        for (int i = 0; i < size; i++) {
          plans.add(new SQPlan(searchParameters.get(i)));
        }
      } else {
        for(int i = 0; i < size; i++) {
          for (List<Word> wds : words) {
            plans.add(new SQPlan(searchParameters.get(i), wds));
          }
        }
      }
      return plans;
    }
  }

  public static List<SQPlan> makePlanEx(SearchParameterOrigin sp, Set<List<Word>> words) throws
    Exception {
    SearchParameter searchParameter;
    List<SearchParameter> searchParameters = sp.splitByType();
    int size = searchParameters.size();
    List<SQPlan> plans = new ArrayList<>();
    if (size == 0) {
      return plans;
    } else {
      if (CollectionUtils.isEmpty(words)) {
        for (int i = 0; i < size; i++) {
          searchParameter = searchParameters.get(i);
          plans.add(new SQPlan(searchParameter));
          if((searchParameter.getType2() == 2375) &&
            (MutableCaches.getInstance().CITIES.getCacheContent().containsKey(searchParameter.getDestination()))){
            SearchParameter sp1 = sp.constructSearchParameterEx(0,2375);
            if(sp1 != null)
              plans.add(new SQPlan(sp1));
          }else if((searchParameter.getType1() == 1785) &&
            (MutableCaches.getInstance().CITIES.getCacheContent().containsKey(searchParameter.getDestination()))){
            SearchParameter sp1 = sp.constructSearchParameterEx(1785,0);
            if(sp1 != null)
              plans.add(new SQPlan(sp1));
          }
        }
      } else {
        int pos = -1;
        for(int i = 0; i < size; i++) {
          for (List<Word> wds : words) {
            searchParameter = searchParameters.get(i);
            plans.add(new SQPlan(searchParameter, wds));
            if(wds.size() == 2){
              if(((wds.get(0).getFirstImpression() == Impression.IMPRESSION_CITY)&&(wds.get(1).getName().equals("wifi")))){
                pos = 0;
                SearchParameter sp1 = sp.constructSearchParameterEx(searchParameter.getType1(),
                                                             searchParameter.getType2(),
                                                             wds.get(0).getName());
                if(sp1 != null){
                  List<Word> list = new ArrayList<>();
                  list.add(wds.get(1));
                  plans.add(new SQPlan(sp1, list));
                }
              }
              if(((wds.get(1).getFirstImpression() == Impression.IMPRESSION_CITY) && (wds.get(0).getName().equals("wifi")))){
                pos = 1;
                SearchParameter sp1 = sp.constructSearchParameterEx(searchParameter.getType1(),
                                                             searchParameter.getType2(),
                                                             wds.get(1).getName());
                if(sp1 != null){
                  List<Word> list = new ArrayList<>();
                  list.add(wds.get(0));
                  plans.add(new SQPlan(sp1, list));
                }
              }

              //签证
              if(((wds.get(0).getFirstImpression() == Impression.IMPRESSION_CITY)&&(wds.get(1).getName().equals("签证")))){
                pos = 0;
                SearchParameter sp1 = sp.constructSearchParameterEx(searchParameter.getType1(),
                                                                    searchParameter.getType2(),
                                                                    wds.get(0).getName());
                if(sp1 != null){
                  List<Word> list = new ArrayList<>();
                  list.add(wds.get(1));
                  plans.add(new SQPlan(sp1, list));
                }
              }
              if(((wds.get(1).getFirstImpression() == Impression.IMPRESSION_CITY) && (wds.get(0).getName().equals("签证")))){
                pos = 1;
                SearchParameter sp1 = sp.constructSearchParameterEx(searchParameter.getType1(),
                                                                    searchParameter.getType2(),
                                                                    wds.get(1).getName());
                if(sp1 != null){
                  List<Word> list = new ArrayList<>();
                  list.add(wds.get(0));
                  plans.add(new SQPlan(sp1, list));
                }
              }

              /*if(pos >= 0){
                wds.remove(pos);
                pos = -1;
              }*/
            }
          }
        }
      }
      return plans;
    }
  }

  public static List<SQPlan> makePlanEx2(SearchParameterOrigin sp, Set<List<Word>> words) throws
    Exception {
    SearchParameter searchParameter;
    List<SearchParameter> searchParameters = sp.splitByType();
    int size = searchParameters.size();
    List<SQPlan> plans = new ArrayList<>();
    if (size == 0) {
      return plans;
    } else {
      if (CollectionUtils.isEmpty(words)) {
        for (int i = 0; i < size; i++) {
          searchParameter = searchParameters.get(i);
          plans.add(new SQPlan(searchParameter));
          if((searchParameter.getType2() == 2375) &&
            (MutableCaches.getInstance().CITIES.getCacheContent().containsKey(searchParameter.getDestination()))){
            List<SearchParameter> sps = sp.constructSearchParameterEx2(0, 2375);
            if(CollectionUtils.isNotEmpty(sps)) {
              for(SearchParameter searchParameter1 : sps) {
                plans.add(new SQPlan(searchParameter1));
              }
            }
          }else if((searchParameter.getType1() == 1785) &&
            (MutableCaches.getInstance().CITIES.getCacheContent().containsKey(searchParameter.getDestination()))){
            List<SearchParameter> sps = sp.constructSearchParameterEx2(1785, 0);
            if(CollectionUtils.isNotEmpty(sps)) {
              for(SearchParameter searchParameter1 : sps) {
                plans.add(new SQPlan(searchParameter1));
              }
            }
          }
        }
      } else {
        int pos = -1;
        for(int i = 0; i < size; i++) {
          for (List<Word> wds : words) {
            searchParameter = searchParameters.get(i);
            plans.add(new SQPlan(searchParameter, wds));
            if(wds.size() == 2){
              if(((wds.get(0).getFirstImpression() == Impression.IMPRESSION_CITY)&&(wds.get(1).getName().equals("wifi")))){
                pos = 0;
                List<SearchParameter> sps = sp.constructSearchParameterEx2(
                  searchParameter.getType1(), searchParameter.getType2(), wds.get(0).getName());
                if(CollectionUtils.isNotEmpty(sps)){
                  List<Word> list = new ArrayList<>();
                  list.add(wds.get(1));
                  for (SearchParameter searchParameter1 : sps) {
                    plans.add(new SQPlan(searchParameter1, list));
                  }
                }
              }
              if(((wds.get(1).getFirstImpression() == Impression.IMPRESSION_CITY) && (wds.get(0).getName().equals("wifi")))){
                pos = 1;
                List<SearchParameter> sps = sp.constructSearchParameterEx2(
                  searchParameter.getType1(), searchParameter.getType2(), wds.get(1).getName());
                if(CollectionUtils.isNotEmpty(sps)){
                  List<Word> list = new ArrayList<>();
                  list.add(wds.get(0));
                  for(SearchParameter searchParameter1 : sps) {
                    plans.add(new SQPlan(searchParameter1, list));
                  }
                }
              }

              //签证
              if(((wds.get(0).getFirstImpression() == Impression.IMPRESSION_CITY)&&(wds.get(1).getName().equals("签证")))){
                pos = 0;
                List<SearchParameter> sps = sp.constructSearchParameterEx2(
                  searchParameter.getType1(), searchParameter.getType2(), wds.get(0).getName());
                if(CollectionUtils.isNotEmpty(sps)){
                  List<Word> list = new ArrayList<>();
                  list.add(wds.get(1));
                  for(SearchParameter searchParameter1 : sps) {
                    plans.add(new SQPlan(searchParameter1, list));
                  }
                }
              }
              if(((wds.get(1).getFirstImpression() == Impression.IMPRESSION_CITY) && (wds.get(0).getName().equals("签证")))){
                pos = 1;
                List<SearchParameter> sps = sp.constructSearchParameterEx2(
                  searchParameter.getType1(), searchParameter.getType2(), wds.get(1).getName());
                if(CollectionUtils.isNotEmpty(sps)){
                  List<Word> list = new ArrayList<>();
                  list.add(wds.get(0));
                  for(SearchParameter searchParameter1 : sps) {
                    plans.add(new SQPlan(searchParameter1, list));
                  }
                }
              }

              /*if(pos >= 0){
                wds.remove(pos);
                pos = -1;
              }*/
            }
          }
        }
      }
      return plans;
    }
  }


  public static List<SQPlan> makePlanEx3(SearchParameterOrigin sp, Set<List<Word>> words) throws
    Exception {
    SearchParameter searchParameter;
    List<SearchParameter> searchParameters = sp.splitByTypeEx();
    int size = searchParameters.size();
    List<SQPlan> plans = new ArrayList<>();
    if (size == 0) {
      return plans;
    } else {
      if (CollectionUtils.isEmpty(words)) {
        //System.out.println("WORD为空");
        for (int i = 0; i < size; i++) {
          searchParameter = searchParameters.get(i);
          plans.add(new SQPlan(searchParameter));
          if((searchParameter.getType2() == 2375) && (searchParameter.hasCityDest())){
            String city = searchParameter.getDestination();
            if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
              List<SearchParameter> sps = sp.constructSearchParameterEx3(0, 2375);
              if(CollectionUtils.isNotEmpty(sps)) {
                for(SearchParameter searchParameter1 : sps) {
                  plans.add(new SQPlan(searchParameter1));
                }
              }
            }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
              plans.add(new SQPlan(sp.getTaiwanSP(0, 2375)));
            }
          }else if((searchParameter.getType1() == 1785) && (searchParameter.hasCityDest())){
            String city = searchParameter.getDestination();
            if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
              List<SearchParameter> sps = sp.constructSearchParameterEx3(1785, 0);
              if(CollectionUtils.isNotEmpty(sps)) {
                for(SearchParameter searchParameter1 : sps) {
                  plans.add(new SQPlan(searchParameter1));
                }
              }
            }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
              plans.add(new SQPlan(sp.getTaiwanSP(1785, 0)));
            }
          }
        }
      } else {
        int pos = -1;
        for(int i = 0; i < size; i++) {
          for (List<Word> wds : words) {
            searchParameter = searchParameters.get(i);
            plans.add(new SQPlan(searchParameter, wds));
            if(wds.size() == 1){
              //System.out.println("WORD为1");
              Word word = wds.get(0);
              String name = word.getName();
              if(name != null){
                if(Constants.EXTEND_SET.contains(name)){
                  if(searchParameter.hasCityDest()){
                    String city = searchParameter.getDestination();
                    if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
                      List<SearchParameter> sps =
                        sp.constructSearchParameterEx3(searchParameter.getType1(), searchParameter.getType2());
                      if(CollectionUtils.isNotEmpty(sps)) {
                        for(SearchParameter searchParameter1 : sps) {
                          plans.add(new SQPlan(searchParameter1, wds));
                        }
                      }
                    }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
                      plans.add(new SQPlan(sp.getTaiwanSP(searchParameter.getType1(), searchParameter.getType2()), wds));
                    }
                  }
                }
              }
            }
          }
        }
      }
      return plans;
    }
  }

  //相关搜索
  public static List<SQPlan> makePlanEx4(SearchParameterOrigin sp, Set<List<Word>> words) throws
    Exception {
    SearchParameter searchParameter;
    List<SearchParameter> searchParameters = sp.splitByTypeEx();
    int size = searchParameters.size();
    List<SQPlan> plans = new ArrayList<>();
    if (size == 0) {
      return plans;
    } else {
      if (CollectionUtils.isEmpty(words)) {
        //System.out.println("WORD为空");
        for (int i = 0; i < size; i++) {
          //System.out.println("WORD为空~");
          searchParameter = searchParameters.get(i);
          plans.add(new SQPlan(searchParameter));
          if(searchParameter.isHoelCategory()){
            //System.out.println("酒店");
            Word word = new Word("赠酒店");
            List<Word> list = new ArrayList<>(1);
            list.add(word);
            plans.add(new SQPlan(searchParameter.newObject(), list));
          }
          if((searchParameter.getType2() == 2375) && (searchParameter.hasCityDest())){
            String city = searchParameter.getDestination();
            if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
              List<SearchParameter> sps = sp.constructSearchParameterEx3(0, 2375);
              if(CollectionUtils.isNotEmpty(sps)) {
                for(SearchParameter searchParameter1 : sps) {
                  plans.add(new SQPlan(searchParameter1));
                }
              }
            }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
              plans.add(new SQPlan(sp.getTaiwanSP(0, 2375)));
            }
          }else if((searchParameter.getType1() == 1785) && (searchParameter.hasCityDest())){
            String city = searchParameter.getDestination();
            if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
              List<SearchParameter> sps = sp.constructSearchParameterEx3(1785, 0);
              if(CollectionUtils.isNotEmpty(sps)) {
                for(SearchParameter searchParameter1 : sps) {
                  plans.add(new SQPlan(searchParameter1));
                }
              }
            }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
              plans.add(new SQPlan(sp.getTaiwanSP(1785, 0)));
            }
          }
        }
      } else {
        int pos = -1;
        for(int i = 0; i < size; i++) {
          for (List<Word> wds : words) {
            searchParameter = searchParameters.get(i);
            plans.add(new SQPlan(searchParameter, wds));
            if(wds.size() == 1){
              //System.out.println("WORD为1");
              Word word = wds.get(0);
              String name = word.getName();
              if(name != null){
                if(Constants.EXTEND_SET.contains(name)){
                  if(searchParameter.hasCityDest()){
                    String city = searchParameter.getDestination();
                    if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
                      List<SearchParameter> sps =
                        sp.constructSearchParameterEx3(searchParameter.getType1(), searchParameter.getType2());
                      if(CollectionUtils.isNotEmpty(sps)) {
                        for(SearchParameter searchParameter1 : sps) {
                          plans.add(new SQPlan(searchParameter1, wds));
                        }
                      }
                    }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
                      plans.add(new SQPlan(sp.getTaiwanSP(searchParameter.getType1(), searchParameter.getType2()), wds));
                    }
                  }
                }
              }
            }
          }
        }
      }
      return plans;
    }
  }

  public static List<SQPlan2> doPlan(SearchParameterEntity sp, Set<List<Word>> words, int channel) throws
    Exception {
    SearchParameter searchParameter;
    List<SearchParameter> searchParameters = sp.splitByType(channel);
    int size = searchParameters.size();
    List<SQPlan2> plans = new ArrayList<>();
    ExtendVType extendVType = null;
    if(Constants.Channel_EX_MAP.containsKey(channel)){
      extendVType = Constants.Channel_EX_MAP.get(channel);
    }else {
      extendVType = new ExtendVType();
    }
    //ExtendVType extendVType = Constants.Channel_EX_MAP.get(channel);

    if (size == 0) {
      return plans;
    } else {
      if (CollectionUtils.isEmpty(words)) {
        for (int i = 0; i < size; i++) {
          searchParameter = searchParameters.get(i);
          plans.add(new SQPlan2(searchParameter, channel));
          if(extendVType.needExtend(searchParameter)
            && (searchParameter.hasCityDest())){
            String city = searchParameter.getDestination();
            if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
              List<SearchParameter> sps = sp.extendSearchParameter(searchParameter.getType1(),
                                                                   searchParameter.getType2());
              if(CollectionUtils.isNotEmpty(sps)) {
                for(SearchParameter searchParameter1 : sps) {
                  plans.add(new SQPlan2(searchParameter1, channel));
                }
              }
            }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
              plans.add(new SQPlan2(sp.extendTaiwanSP(searchParameter.getType1(),
                                                      searchParameter.getType2()), channel));
            }
          }
        }
      } else {
        int pos = -1;
        for(int i = 0; i < size; i++) {
          for (List<Word> wds : words) {
            searchParameter = searchParameters.get(i);
            plans.add(new SQPlan2(searchParameter, wds, channel));
            if(wds.size() == 1){
              //System.out.println("WORD为1");
              Word word = wds.get(0);
              String name = word.getName();
              if(name != null){
                if(Constants.EXTEND_SET.contains(name)){
                  if(searchParameter.hasCityDest()){
                    String city = searchParameter.getDestination();
                    if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
                      List<SearchParameter> sps =
                        sp.extendSearchParameter(searchParameter.getType1(),
                                                 searchParameter.getType2());
                      if(CollectionUtils.isNotEmpty(sps)) {
                        for(SearchParameter searchParameter1 : sps) {
                          plans.add(new SQPlan2(searchParameter1, wds, channel));
                        }
                      }
                    }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
                      plans.add(new SQPlan2(sp.extendTaiwanSP(searchParameter.getType1(),
                                                              searchParameter.getType2()), wds, channel));
                    }
                  }
                }
              }
            }
          }
        }
      }
      return plans;
    }
  }

  /////////////////
  public static List<SQPlan2> doPlan2(SearchParameterEntity sp, Set<List<Word>> words, int channel) throws
    Exception {
    SearchParameter searchParameter;
    List<SearchParameter> searchParameters = sp.splitByType(channel);
    int size = searchParameters.size();
    List<SQPlan2> plans = new ArrayList<>();
    ExtendVType extendVType = null;
    if(Constants.Channel_EX_MAP.containsKey(channel)){
      extendVType = Constants.Channel_EX_MAP.get(channel);
    }else {
      extendVType = new ExtendVType();
    }
    //System.out.println("do plan 2");
    //ExtendVType extendVType = Constants.Channel_EX_MAP.get(channel);
    try {
      //System.out.println("words size:" + words.size());
      //System.out.println("words:"+words);
    }catch (Exception e){
      e.printStackTrace();
    }
    if (size == 0) {
      return plans;
    } else {
      if (CollectionUtils.isEmpty(words)) {
        for (int i = 0; i < size; i++) {
          searchParameter = searchParameters.get(i);
          plans.add(new SQPlan2(searchParameter, channel));
          if(extendVType.needExtend(searchParameter)
            && (searchParameter.hasCityDest())){
            String city = searchParameter.getDestination();
            if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
              List<SearchParameter> sps = sp.extendSearchParameter(searchParameter.getType1(),
                                                                   searchParameter.getType2());
              if(CollectionUtils.isNotEmpty(sps)) {
                for(SearchParameter searchParameter1 : sps) {
                  plans.add(new SQPlan2(searchParameter1, channel));
                }
              }
            }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
              plans.add(new SQPlan2(sp.extendTaiwanSP(searchParameter.getType1(),
                                                      searchParameter.getType2()), channel));
            }
          }else {
            //System.out.println("直接扩展准备  size:"+size);
            if(size == 1){

              int stg = searchParameter.getType1();
              int st = searchParameter.getType2();
              //System.out.println("直接扩展准备2 stg:"+stg+"  st:"+st);
              if((stg == 0) && (st == 0)){
                //System.out.println("城市目的地直接扩展");
                if(searchParameter.hasCityDest()){
                  //System.out.println("城市目的地直接扩展》》》");
                  String city = searchParameter.getDestination();
                  if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
                    List<SearchParameter> sps = sp.extendSearchParameter(extendVType);
                    //System.out.println("非台湾城市直接扩展size:" + sps.size());
                    if(CollectionUtils.isNotEmpty(sps)) {
                      for(SearchParameter searchParameter1 : sps) {
                        plans.add(new SQPlan2(searchParameter1, channel));
                      }
                    }
                  }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
                    List<SearchParameter> sps = sp.extendTaiwanSPList(extendVType);
                    //System.out.println("台湾城市直接扩展size:" + sps.size());
                    if(CollectionUtils.isNotEmpty(sps)) {
                      for(SearchParameter searchParameter1 : sps) {
                        plans.add(new SQPlan2(searchParameter1, channel));
                      }
                    }
                  }
                }
              }
            }
          }
        }
      } else {
        int pos = -1;
        for(int i = 0; i < size; i++) {
          for (List<Word> wds : words) {
            searchParameter = searchParameters.get(i);
            plans.add(new SQPlan2(searchParameter, wds, channel));
            if(wds.size() == 1){
              //System.out.println("WORD为1");
              Word word = wds.get(0);
              String name = word.getName();
              if(name != null){
                if(Constants.EXTEND_SET.contains(name)){
                  if(searchParameter.hasCityDest()){
                    String city = searchParameter.getDestination();
                    if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
                      List<SearchParameter> sps =
                        sp.extendSearchParameter(searchParameter.getType1(),
                                                 searchParameter.getType2());
                      if(CollectionUtils.isNotEmpty(sps)) {
                        for(SearchParameter searchParameter1 : sps) {
                          plans.add(new SQPlan2(searchParameter1, wds, channel));
                        }
                      }
                    }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
                      plans.add(new SQPlan2(sp.extendTaiwanSP(searchParameter.getType1(),
                                                              searchParameter.getType2()), wds, channel));
                    }
                  }
                }
              }
            }
          }
        }
      }
      return plans;
    }
  }


  public static List<SQPlan2> doPlan3(SearchParameterEntity sp, Set<List<Word>> words, int channel) throws
    Exception {
    SearchParameter searchParameter;
    List<SearchParameter> searchParameters = sp.splitByType(channel);
    int size = searchParameters.size();
    List<SQPlan2> plans = new ArrayList<>();
    ExtendVType extendVType = null;
    Map<Integer, List<ExtendEntity>> extendEntityMap = null;
    if(Constants.Channel_EX_MAP.containsKey(channel)){
      extendVType = Constants.Channel_EX_MAP.get(channel);
    }else {
      extendVType = new ExtendVType();
    }

    if(Constants.Channel_EX_MAP_2.containsKey(channel)){
      extendEntityMap = Constants.Channel_EX_MAP_2.get(channel);
    }else {
      extendEntityMap = new HashMap<>();
    }
    //System.out.println("do plan 2");
    //ExtendVType extendVType = Constants.Channel_EX_MAP.get(channel);
    try {
      //System.out.println("words size:" + words.size());
      //System.out.println("words:"+words);
    }catch (Exception e){
      e.printStackTrace();
    }
    if (size == 0) {
      return plans;
    } else {
      if (CollectionUtils.isEmpty(words)) {
        for (int i = 0; i < size; i++) {
          searchParameter = searchParameters.get(i);
          plans.add(new SQPlan2(searchParameter, channel));
          if(extendVType.needExtend(searchParameter)
            && (searchParameter.hasCityDest())){
            String city = searchParameter.getDestination();
            if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
              List<SearchParameter> sps = sp.extendSearchParameter(searchParameter.getType1(),
                                                                   searchParameter.getType2());
              if(CollectionUtils.isNotEmpty(sps)) {
                for(SearchParameter searchParameter1 : sps) {
                  plans.add(new SQPlan2(searchParameter1, channel));
                }
              }
            }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
              plans.add(new SQPlan2(sp.extendTaiwanSP(searchParameter.getType1(),
                                                      searchParameter.getType2()), channel));
            }
          }if(isExtend(searchParameter, extendEntityMap) && searchParameter.hasCityDest()){
            //////////////////////////////////////////////////////////////
            System.out.println("进来了  扩展");
            String city = searchParameter.getDestination();
            if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
              int stg = searchParameter.getType1();
              List<ExtendEntity> extendList = extendEntityMap.get(stg);
              for(ExtendEntity extendEntity : extendList) {
                int t1 = extendEntity.getType1();
                int t2 = extendEntity.getType2();
                List<SearchParameter> sps = sp
                  .extendSearchParameter(t1, t2);
                if (CollectionUtils.isNotEmpty(sps)) {
                  for (SearchParameter searchParameter1 : sps) {
                    plans.add(new SQPlan2(searchParameter1, channel));
                  }
                }
              }
            }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
              int stg = searchParameter.getType1();
              List<ExtendEntity> extendList = extendEntityMap.get(stg);
              for(ExtendEntity extendEntity : extendList) {
                int t1 = extendEntity.getType1();
                int t2 = extendEntity.getType2();
                plans.add(new SQPlan2(sp.extendTaiwanSP(t1, t2), channel));
              }
              plans.add(new SQPlan2(sp.extendTaiwanSP(searchParameter.getType1(),
                                                      searchParameter.getType2()), channel));
            }
            //////////////////////////////////////////////////////////////
          } else {
            //System.out.println("直接扩展准备  size:"+size);
            if(size == 1){

              int stg = searchParameter.getType1();
              int st = searchParameter.getType2();
              //System.out.println("直接扩展准备2 stg:"+stg+"  st:"+st);
              if((stg == 0) && (st == 0)){
                //System.out.println("城市目的地直接扩展");
                if(searchParameter.hasCityDest()){
                  //System.out.println("城市目的地直接扩展》》》");
                  String city = searchParameter.getDestination();
                  if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
                    List<SearchParameter> sps = sp.extendSearchParameter(extendVType);
                    //System.out.println("非台湾城市直接扩展size:" + sps.size());
                    if(CollectionUtils.isNotEmpty(sps)) {
                      for(SearchParameter searchParameter1 : sps) {
                        plans.add(new SQPlan2(searchParameter1, channel));
                      }
                    }
                  }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
                    List<SearchParameter> sps = sp.extendTaiwanSPList(extendVType);
                    //System.out.println("台湾城市直接扩展size:" + sps.size());
                    if(CollectionUtils.isNotEmpty(sps)) {
                      for(SearchParameter searchParameter1 : sps) {
                        plans.add(new SQPlan2(searchParameter1, channel));
                      }
                    }
                  }
                }
              }
            }
          }
        }
      } else {
        int pos = -1;
        for(int i = 0; i < size; i++) {
          for (List<Word> wds : words) {
            searchParameter = searchParameters.get(i);
            plans.add(new SQPlan2(searchParameter, wds, channel));
            if(wds.size() == 1){
              //System.out.println("WORD为1");
              Word word = wds.get(0);
              String name = word.getName();
              if(name != null){
                if(Constants.EXTEND_SET.contains(name)){
                  if(searchParameter.hasCityDest()){
                    String city = searchParameter.getDestination();
                    if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
                      List<SearchParameter> sps =
                        sp.extendSearchParameter(searchParameter.getType1(),
                                                 searchParameter.getType2());
                      if(CollectionUtils.isNotEmpty(sps)) {
                        for(SearchParameter searchParameter1 : sps) {
                          plans.add(new SQPlan2(searchParameter1, wds, channel));
                        }
                      }
                    }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
                      plans.add(new SQPlan2(sp.extendTaiwanSP(searchParameter.getType1(),
                                                              searchParameter.getType2()), wds, channel));
                    }
                  }
                }
              }
            }
          }
        }
      }
      return plans;
    }
  }

  public static List<SQPlan2> doPlan4(SearchParameterEntity sp, Set<List<Word>> words, int channel) throws
    Exception {
    SearchParameter searchParameter;
    List<SearchParameter> searchParameters = sp.splitByType(channel);
    int size = searchParameters.size();
    List<SQPlan2> plans = new ArrayList<>();

    Set<Integer> stypeSet = MutableCaches.getInstance().Z_TAG_SNAPSHOT.getCacheContent()
                                                                      .getvStypeMap()
                                                                      .get(channel).getExtend_st();
    Set<Integer> stgSet = MutableCaches.getInstance().Z_TAG_SNAPSHOT.getCacheContent()
                                                                    .getvStypeMap()
                                                                    .get(channel).getExtend_stg();
    ExtendVType extendVType = new ExtendVType(stgSet, stypeSet);
    Map<Integer, List<ExtendEntity>> extendListMap = MutableCaches.getInstance().Z_TAG_SNAPSHOT.getCacheContent()
                                                                                               .getvStypeMap()
                                                                                               .get(
                                                                                                 channel)
                                                                                               .getExtend_half();
    //System.out.println("do plan 2");
    //ExtendVType extendVType = Constants.Channel_EX_MAP.get(channel);
    /*try {
      //System.out.println("words size:" + words.size());
      //System.out.println("words:"+words);
    }catch (Exception e){
      e.printStackTrace();
    }*/
    if (size == 0) {
      return plans;
    } else {
      if (CollectionUtils.isEmpty(words)) {
        for (int i = 0; i < size; i++) {
          searchParameter = searchParameters.get(i);
          plans.add(new SQPlan2(searchParameter, channel));
          if(needExtend(searchParameter, stypeSet, stgSet)
            && (searchParameter.hasCityDest())){
            String city = searchParameter.getDestination();
            if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
              List<SearchParameter> sps = sp.extendSearchParameter(searchParameter.getType1(),
                                                                   searchParameter.getType2());
              if(CollectionUtils.isNotEmpty(sps)) {
                for(SearchParameter searchParameter1 : sps) {
                  plans.add(new SQPlan2(searchParameter1, channel));
                }
              }
            }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
              plans.add(new SQPlan2(sp.extendTaiwanSP(searchParameter.getType1(),
                                                      searchParameter.getType2()), channel));
            }
          }if(isExtend(searchParameter, extendListMap) && searchParameter.hasCityDest()){
            //////////////////////////////////////////////////////////////
            //System.out.println("进来了  扩展");
            String city = searchParameter.getDestination();
            if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
              int stg = searchParameter.getType1();
              List<ExtendEntity> extendList = extendListMap.get(stg);
              for(ExtendEntity extendEntity : extendList) {
                int t1 = extendEntity.getType1();
                int t2 = extendEntity.getType2();
                List<SearchParameter> sps = sp
                  .extendSearchParameter(t1, t2);
                if (CollectionUtils.isNotEmpty(sps)) {
                  for (SearchParameter searchParameter1 : sps) {
                    plans.add(new SQPlan2(searchParameter1, channel));
                  }
                }
              }
            }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
              int stg = searchParameter.getType1();
              List<ExtendEntity> extendList = extendListMap.get(stg);
              for(ExtendEntity extendEntity : extendList) {
                int t1 = extendEntity.getType1();
                int t2 = extendEntity.getType2();
                plans.add(new SQPlan2(sp.extendTaiwanSP(t1, t2), channel));
              }
              plans.add(new SQPlan2(sp.extendTaiwanSP(searchParameter.getType1(),
                                                      searchParameter.getType2()), channel));
            }
            //////////////////////////////////////////////////////////////
          } else {
            //System.out.println("直接扩展准备  size:"+size);
            if(size == 1){

              int stg = searchParameter.getType1();
              int st = searchParameter.getType2();
              //System.out.println("直接扩展准备2 stg:"+stg+"  st:"+st);
              if((stg == 0) && (st == 0)){
                //System.out.println("城市目的地直接扩展");
                if(searchParameter.hasCityDest()){
                  //System.out.println("城市目的地直接扩展》》》");
                  String city = searchParameter.getDestination();
                  if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
                    List<SearchParameter> sps = sp.extendSearchParameter(extendVType);
                    //System.out.println("非台湾城市直接扩展size:" + sps.size());
                    if(CollectionUtils.isNotEmpty(sps)) {
                      for(SearchParameter searchParameter1 : sps) {
                        plans.add(new SQPlan2(searchParameter1, channel));
                      }
                    }
                  }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
                    List<SearchParameter> sps = sp.extendTaiwanSPList(extendVType);
                    //System.out.println("台湾城市直接扩展size:" + sps.size());
                    if(CollectionUtils.isNotEmpty(sps)) {
                      for(SearchParameter searchParameter1 : sps) {
                        plans.add(new SQPlan2(searchParameter1, channel));
                      }
                    }
                  }
                }
              }
            }
          }
        }
      } else {
        int pos = -1;
        for(int i = 0; i < size; i++) {
          for (List<Word> wds : words) {
            searchParameter = searchParameters.get(i);
            plans.add(new SQPlan2(searchParameter, wds, channel));
            if(wds.size() == 1){
              //System.out.println("WORD为1");
              Word word = wds.get(0);
              String name = word.getName();
              if(name != null){
                if(Constants.EXTEND_SET.contains(name)){
                  if(searchParameter.hasCityDest()){
                    String city = searchParameter.getDestination();
                    if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
                      List<SearchParameter> sps =
                        sp.extendSearchParameter(searchParameter.getType1(),
                                                 searchParameter.getType2());
                      if(CollectionUtils.isNotEmpty(sps)) {
                        for(SearchParameter searchParameter1 : sps) {
                          plans.add(new SQPlan2(searchParameter1, wds, channel));
                        }
                      }
                    }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
                      plans.add(new SQPlan2(sp.extendTaiwanSP(searchParameter.getType1(),
                                                              searchParameter.getType2()), wds, channel));
                    }
                  }
                }
              }
            }
          }
        }
      }
      return plans;
    }
  }


  public static List<SQPlan3> doPlan5(SearchParameterEntity sp, Set<List<Word>> words, int channel) throws
    Exception {
    SearchParameter searchParameter;
    List<SearchParameter> searchParameters = sp.splitByType2(channel);
    int size = searchParameters.size();
    List<SQPlan3> plans = new ArrayList<>();

    Set<Integer> stypeSet = MutableCaches.getInstance().Z_TAG_SNAPSHOT.getCacheContent()
                                                                      .getvStypeMap()
                                                                      .get(channel).getExtend_st();
    Set<Integer> stgSet = MutableCaches.getInstance().Z_TAG_SNAPSHOT.getCacheContent()
                                                                    .getvStypeMap()
                                                                    .get(channel).getExtend_stg();
    ExtendVType extendVType = new ExtendVType(stgSet, stypeSet);
    Map<Integer, List<ExtendEntity>> extendListMap = MutableCaches.getInstance().Z_TAG_SNAPSHOT.getCacheContent()
                                                                                               .getvStypeMap()
                                                                                               .get(
                                                                                                 channel)
                                                                                               .getExtend_half();
    //System.out.println("do plan 2");
    //ExtendVType extendVType = Constants.Channel_EX_MAP.get(channel);
    /*try {
      //System.out.println("words size:" + words.size());
      //System.out.println("words:"+words);
    }catch (Exception e){
      e.printStackTrace();
    }*/
    if (size == 0) {
      return plans;
    } else {
      if (CollectionUtils.isEmpty(words)) {
        for (int i = 0; i < size; i++) {
          searchParameter = searchParameters.get(i);
          plans.add(new SQPlan3(searchParameter, channel));
          if(needExtend(searchParameter, stypeSet, stgSet)
            && (searchParameter.hasCityDest())){
            String city = searchParameter.getDestination();
            if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
              List<SearchParameter> sps = sp.extendSearchParameterWithContinent(
                searchParameter.getType1(), searchParameter.getType2());
              if(CollectionUtils.isNotEmpty(sps)) {
                for(SearchParameter searchParameter1 : sps) {
                  plans.add(new SQPlan3(searchParameter1, channel));
                }
              }
            }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
              //System.out.println("1");
              plans.add(new SQPlan3(sp.extendTaiwanSPWithContinent(searchParameter.getType1(),
                                                                   searchParameter.getType2()), channel));
            }
          }if(isExtend(searchParameter, extendListMap) && searchParameter.hasCityDest()){
            //////////////////////////////////////////////////////////////
            //System.out.println("进来了  扩展");
            String city = searchParameter.getDestination();
            if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
              int stg = searchParameter.getType1();
              List<ExtendEntity> extendList = extendListMap.get(stg);
              for(ExtendEntity extendEntity : extendList) {
                int t1 = extendEntity.getType1();
                int t2 = extendEntity.getType2();
                List<SearchParameter> sps = sp
                  .extendSearchParameterWithContinent(t1, t2);
                if (CollectionUtils.isNotEmpty(sps)) {
                  for (SearchParameter searchParameter1 : sps) {
                    plans.add(new SQPlan3(searchParameter1, channel));
                  }
                }
              }
            }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
              int stg = searchParameter.getType1();
              List<ExtendEntity> extendList = extendListMap.get(stg);
              for(ExtendEntity extendEntity : extendList) {
                int t1 = extendEntity.getType1();
                int t2 = extendEntity.getType2();
                plans.add(new SQPlan3(sp.extendTaiwanSPWithContinent(t1, t2), channel));
              }
              //plans.add(new SQPlan3(sp.extendTaiwanSPWithContinent(searchParameter.getType1(),
                                                                   //searchParameter.getType2()), channel));
            }
            //////////////////////////////////////////////////////////////
          } else {
            //System.out.println("直接扩展准备  size:"+size);
            if(size == 1){

              int stg = searchParameter.getType1();
              int st = searchParameter.getType2();
              //System.out.println("直接扩展准备2 stg:"+stg+"  st:"+st);
              if((stg == 0) && (st == 0)){
                //System.out.println("城市目的地直接扩展");
                if(searchParameter.hasCityDest()){
                  //System.out.println("城市目的地直接扩展》》》");
                  String city = searchParameter.getDestination();
                  if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
                    List<SearchParameter> sps = sp.extendSearchParameterWithContinent(extendVType);
                    //System.out.println("非台湾城市直接扩展size:" + sps.size());
                    if(CollectionUtils.isNotEmpty(sps)) {
                      for(SearchParameter searchParameter1 : sps) {
                        plans.add(new SQPlan3(searchParameter1, channel));
                      }
                    }
                  }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
                    List<SearchParameter> sps = sp.extendTaiwanSPListWithContinent(extendVType);
                    //System.out.println("台湾城市直接扩展size:" + sps.size());
                    if(CollectionUtils.isNotEmpty(sps)) {
                      for(SearchParameter searchParameter1 : sps) {
                        plans.add(new SQPlan3(searchParameter1, channel));
                      }
                    }
                  }
                }
              }
            }
          }
        }
      } else {
        int pos = -1;
        for(int i = 0; i < size; i++) {
          for (List<Word> wds : words) {
            searchParameter = searchParameters.get(i);
            plans.add(new SQPlan3(searchParameter, wds, channel));
            if(wds.size() == 1){
              //System.out.println("WORD为1");
              Word word = wds.get(0);
              String name = word.getName();
              if(name != null){
                if(Constants.EXTEND_SET.contains(name)){
                  if(searchParameter.hasCityDest()){
                    String city = searchParameter.getDestination();
                    if(MutableCaches.getInstance().CITY_COUNTRY_MAP_LIST.getCacheContent().containsKey(city)){
                      List<SearchParameter> sps =
                        sp.extendSearchParameterWithContinent(searchParameter.getType1(),
                                                              searchParameter.getType2());
                      if(CollectionUtils.isNotEmpty(sps)) {
                        for(SearchParameter searchParameter1 : sps) {
                          plans.add(new SQPlan3(searchParameter1, wds, channel));
                        }
                      }
                    }else if(MutableCaches.getInstance().TAIWAN_MAP.getCacheContent().contains(city)){
                      plans.add(new SQPlan3(sp.extendTaiwanSPWithContinent(searchParameter.getType1(),
                                                              searchParameter.getType2()), wds, channel));
                    }
                  }
                }
              }
            }
          }
        }
      }
      return plans;
    }
  }



  public static boolean isExtend(SearchParameter searchParameter, Map<Integer, List<ExtendEntity>> extendMap){
    int stg = searchParameter.getType1();
    int sty = searchParameter.getType2();
    if((stg != 0) && (sty == 0)){
      if(extendMap.containsKey(stg)){
        return true;
      }
    }
    return false;
  }

  public static boolean needExtend(SearchParameter searchParameter, Set<Integer> stSet, Set<Integer> stgSet){
    int id1 = searchParameter.getType1();
    int id2 = searchParameter.getType2();
    if(stSet.contains(id2)){
      return true;
    }
    if(stgSet.contains(id1)){
      return true;
    }
    return false;
  }

}
